VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmSpatialDataImport 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   " MapPoint 2002 - GIS Spatial Data Import Utility"
   ClientHeight    =   6570
   ClientLeft      =   1275
   ClientTop       =   1575
   ClientWidth     =   7890
   Icon            =   "frmSpatialDataImport.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   6570
   ScaleWidth      =   7890
   ShowInTaskbar   =   0   'False
   StartUpPosition =   1  'CenterOwner
   Begin VB.Frame fraExplaination 
      Caption         =   "Code Sample Description"
      Height          =   3495
      Left            =   120
      TabIndex        =   7
      Top             =   120
      Width           =   7695
      Begin VB.Label Label3 
         Caption         =   $"frmSpatialDataImport.frx":030A
         Height          =   495
         Left            =   120
         TabIndex        =   11
         Top             =   2880
         Width           =   6975
      End
      Begin VB.Label Label1 
         Caption         =   $"frmSpatialDataImport.frx":03B7
         Height          =   1215
         Left            =   120
         TabIndex        =   10
         Top             =   1560
         Width           =   7455
      End
      Begin VB.Label Label2 
         Caption         =   $"frmSpatialDataImport.frx":05D2
         Height          =   1215
         Left            =   120
         TabIndex        =   8
         Top             =   240
         Width           =   7455
      End
   End
   Begin MSComDlg.CommonDialog cdlFileOpen 
      Left            =   4920
      Top             =   6000
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.CommandButton cmdStart 
      Caption         =   "Start"
      Default         =   -1  'True
      Height          =   375
      Left            =   5520
      TabIndex        =   5
      Top             =   6060
      Width           =   1095
   End
   Begin VB.Frame fraStatus 
      Caption         =   "Status"
      Height          =   1395
      Left            =   120
      TabIndex        =   3
      Top             =   4560
      Width           =   7695
      Begin VB.Label lblStatus 
         Height          =   975
         Left            =   120
         TabIndex        =   4
         Top             =   300
         Width           =   7395
      End
   End
   Begin VB.TextBox txtFilename 
      Height          =   315
      Left            =   120
      TabIndex        =   1
      Top             =   4020
      Width           =   6435
   End
   Begin VB.CommandButton cmdBrowse 
      Caption         =   "Browse..."
      Height          =   375
      Left            =   6720
      TabIndex        =   2
      Top             =   4020
      Width           =   1095
   End
   Begin VB.CommandButton cmdClose 
      Cancel          =   -1  'True
      Caption         =   "Close"
      Height          =   375
      Left            =   6720
      TabIndex        =   6
      Top             =   6060
      Width           =   1095
   End
   Begin VB.Label lblRecordCount 
      Height          =   255
      Left            =   240
      TabIndex        =   9
      Top             =   6120
      Width           =   4455
   End
   Begin VB.Label lblFilename 
      Caption         =   "Specify a ESRI or MapInfo file to import:"
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   3780
      Width           =   4935
   End
End
Attribute VB_Name = "frmSpatialDataImport"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'*************************************************************************
'  This source code is provided "as is" and as an unsupported sample of
'  a Microsoft Visual Basic application that incorporates the MapPoint 2002
'  object model and ActiveX control. It can only  be used in accordance with
'  the terms and conditions of the Microsoft MapPoint 2002 and
'  Microsoft Visual Basic End User License Agreements.
'*************************************************************************

Private m_oConnect As Connect
Private m_omMap As MapPoint.Map
Attribute m_omMap.VB_VarHelpID = -1


'**********************************************************************************
' Public Procedures
'**********************************************************************************



Public Sub Display(omMap As MapPoint.Map, _
                   oConnect As Connect)
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: The initial routine that gets called from the addin.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    On Error GoTo errorHandler
    
    'Set a reference to the active map
    Set m_omMap = omMap
    
    'Set a reference to the Connect object.  Need this so we can release the form reference
    'in the Connect object when the form is closed.
    Set m_oConnect = oConnect
    
    Me.Show vbModal
    
    Exit Sub
errorHandler:
    MsgBox "Error in Display" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Sub



'**********************************************************************************
' Event Procedures
'**********************************************************************************



Private Sub Form_Unload(Cancel As Integer)
'**********************************************************************************
    ' Date: 3/13/2001
    ' Abstract: Called when the form is unloaded.  Need to tell the Connect object
    '           to release its reference to the form, so that everything terminates
    '           properly.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    m_oConnect.ReleaseForm

End Sub

Private Sub cmdStart_Click()
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Called when the user clicks on the "Start" button.  Checks to make
    '           sure a valid filename was selected, then begins processing of the
    '           file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim sFilename As String
    
    On Error GoTo errorHandler
    
    'Reset the status display.
    UpdateStatus ""
    
    'Get the filename from the textbox.
    sFilename = Trim$(txtFilename.Text)
    
    'Make sure a filename was specified.  If not, then display an error message.
    If Len(sFilename) = 0 Then
        MsgBox "Please specify a filename."
    Else
        'Make sure the specified file actually exists.  If not, then display an error message.
        If Len(Dir(sFilename)) = 0 Then
            MsgBox "The specified file was not found."
        Else
            'Process the file.
            ImportExternalData sFilename
        End If
    End If
    
    Exit Sub
errorHandler:
    MsgBox "Error in cmdStart_Click" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Sub

Private Sub cmdClose_Click()
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Called when the user clicks on the "Close" button.  Closes the form.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Unload Me
    
End Sub

Private Sub cmdBrowse_Click()
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Called when the user clicks on the Browse... button.  Gets the
    '           selected filename and places the result in the txtFilename
    '           textbox.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim bCancel As Boolean
    Dim sFilename As String
    
    On Error GoTo errorHandler
    
    'Reset the status display.
    UpdateStatus ""
    
    'Get the filename from the Common Dialog control.
    sFilename = GetSelectedFilename(bCancel)
    
    'If the user didn't cancel the Common Dialog, then update the textbox.
    If bCancel = False Then
        txtFilename.Text = Trim$(sFilename)
    End If
    
    Exit Sub
errorHandler:
    MsgBox "Error in cmdBrowse_Click" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Sub




'**********************************************************************************
' Private procedures
'**********************************************************************************




Private Function GetSelectedFilename(ByRef bCancel As Boolean) As String
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Uses the Common-Dialog control to get a selected filename.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim sFilterString As String
    Dim sPath As String
    Dim sFilename As String
    Dim sPathFilename As String
    
    On Error GoTo errorHandler
    
    'Set the filter string to show MapPoint-related files.
    sFilterString = "ESRI Shapefiles (*.shp)|*.shp" _
                 & "|MapInfo Data Files (*.mif)|*.mif" _
                 & "|All Files (*.*)|*.*"
    
    'Parse the path & filename.
    sPathFilename = Trim$(txtFilename.Text)
    ParseFileAndPathName sPathFilename, sPath, sFilename
    
    'If we didn't get a pathname, then set the path to
    'the same location as the compiled dll.
    If Len(sPath) = 0 Then
        sPath = App.Path
    End If
    
    With cdlFileOpen
        'Setup the Common-Dialog control
        .DialogTitle = "Open"
        .Filter = sFilterString
        .FilterIndex = 1
        .InitDir = sPath
        .Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
        .CancelError = True
        
        'Show the dialog.
        .ShowOpen
        
        'Return the selected filename.
        GetSelectedFilename = .FileName
        
    End With
    
    Exit Function
errorHandler:
    If Err.Number = cdlCancel Then
        'User canceled the Open dialog, so return bCancel=True and return a null
        'string for the filename.
        bCancel = True
        GetSelectedFilename = ""
    Else
        MsgBox "Error in GetSelectedFilename" & vbCrLf _
             & "Error Number: " & Err.Number & vbCrLf _
             & "Error Description: " & Err.Description, vbExclamation, App.Title
    End If
End Function

Private Sub ImportExternalData(sFilename As String)
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Parses the given file and generates MapPoint elements that
    '           correspond to the elements in the file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim oShapeGenerator As SpatialDataImport.ShapeGenerator
    Dim bSuccess As Boolean
    
    On Error GoTo errorHandler
    
    Me.MousePointer = vbHourglass
    m_omMap.Application.MousePointer = geoPointerHourglass
    
    'Create a new instance of our ShapeGenerator object.
    Set oShapeGenerator = New SpatialDataImport.ShapeGenerator
    
    'Attempt to generate MapPoint shapes from the given data file.
    UpdateStatus "Getting Shape Data..."
    bSuccess = oShapeGenerator.GenerateShapes(sFilename)
    
    'If we successfully generated shapes, then render those shapes in MapPoint.
    If bSuccess = True Then
        UpdateStatus "Rendering Shapes..."
        oShapeGenerator.RenderShapes m_omMap
        UpdateStatus "Done!"
    Else
        UpdateStatus ""
        MsgBox "Import Failed!" & vbCrLf & vbCrLf _
        & "Either no shape data was found in this file" _
        & " or the shape type you tried to import is not supported by this add in." _
        & " Please review documentation of your native format for further" _
        & " information regarding shape types.", vbCritical + vbOKOnly, "Import Failed"
    End If
    
    Me.MousePointer = vbDefault
    m_omMap.Application.MousePointer = geoPointerDefault
    
    Exit Sub
errorHandler:
    MsgBox "Error in ImportExternalData" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Sub

